package com.zjgeport.zjgdanger.CompanyInfo;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.soar.controller.PullDownView;
import com.soar.util.net.rmi.httpxml.HttpCallBack;
import com.soar.util.net.rmi.httpxml.HttpClient;
import com.soar.util.net.rmi.httpxml.HttpMethod;
import com.soar.util.net.rmi.httpxml.HttpResult;
import com.zjgeport.zjgdanger.CommonActivity;
import com.zjgeport.zjgdanger.LoginActivity;
import com.zjgeport.zjgdanger.R;
import com.zjgeport.zjgdanger.common.ParamCommon;
import com.zjgeport.zjgdanger.model.myApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CompInfoDetailActivity extends CommonActivity {

    private ListView mListView;

    private LinearLayout bottomell;

    private PullDownView mPullDownView;

//    private MyCustomAdapter adapter;

    private List<HashMap<String, String>> permitList = new ArrayList<HashMap<String, String>>();

    private List<HashMap<String, String>> cargoList = new ArrayList<HashMap<String, String>>();

    private String  cargoName ="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comp_info_detail);
        initCommonListener();
        initView();
    }

    private void initView() {

        TextView tv = findTextViewById(R.id.title);
        bottomell = (LinearLayout)findViewById(R.id.bottom_ll);
        tv.setText("码头企业详细信息");

        float scale = getResources().getDisplayMetrics().density;
        int height = (int) (70 * scale + 0.5f);

//        mPullDownView = new PullDownView(this, height);
//        mPullDownView = (PullDownView) this.findViewById(R.id.pull_down_view);
//        mPullDownView.setOnPullDownListener(new PullDownView.OnPullDownListener() {
//
//            public void onRefresh()
//            {
//
//            }
//
//            public void onMore()
//            {
//                Looper mainLooper = Looper.getMainLooper();
//                MyHandler handler = new MyHandler(mainLooper);
//                Message msg = new Message();
//                msg.what = 1999;
//                handler.sendMessage(msg);
//            }
//        });
//        mPullDownView.changeFoot(0, "没有更多信息了");
//        adapter = new MyCustomAdapter(this, permitList);
//        mListView = mPullDownView.getListView();
//        mListView.setDivider(getResources().getDrawable(R.color.white));
//        mListView.setSelector(R.color.ap_sel);// 取消点击背景
//        mListView.setAdapter(adapter);
//        // 设置可以自动获取更多 滑到最后一个自动获取 改成false将禁用自动获取更多
//        mPullDownView.enableAutoFetchMore(false, 1);
//        mPullDownView.notifyDidLoad();
        getWorkRangeList();
        getAccidentDetail();
        getPermitList();

    }

    /**
     * 获取企业资质基础信息
     */
    private void getAccidentDetail() {

        HttpMethod method = new HttpMethod();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("keyword", getIntent().getStringExtra("ID"));
        map.put("TOKEN", myApplication.getUser().getToken());
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("selectCompDetailInfo");
        method.setKvdata(map);
        final List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        HttpClient.postdata(new Button(CompInfoDetailActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {

                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)) {
                    List<HashMap<String, String>> data1 = result.getDataSet();
                    if (data1 != null && data1.size() != 0) {
                        data.addAll(data1);
                    }
                    // 刷新UI
                    Looper mainLooper = Looper.getMainLooper();
                    MyHandler handler = new MyHandler(mainLooper);
                    Message msg = new Message();
                    msg.what = 999;
                    msg.obj = data;
                    handler.sendMessage(msg);

                }
                else {
                    showToast(result.getResultMessage());
                    if (result.getResultCode().equals("90"))
                    {
                        Intent intent = new Intent(CompInfoDetailActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }


            }
        });
    }

    /**
     * 获取企业许可证列表信息
     */

    private void getPermitList() {

        HttpMethod method = new HttpMethod();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("keyword", getIntent().getStringExtra("ID"));
        map.put("TOKEN", myApplication.getUser().getToken());
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("selectPermitListInfo");
        method.setKvdata(map);
        final List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        HttpClient.postdata(new Button(CompInfoDetailActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {

                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)) {
                    List<HashMap<String, String>> data1 = result.getDataSet();
                    if (data1 != null && data1.size() != 0) {
                        data.addAll(data1);
                    }
                    else{
//                        mPullDownView.notifyDidLoad();
                    }
                    // 刷新UI
                    Looper mainLooper = Looper.getMainLooper();
                    MyHandler handler = new MyHandler(mainLooper);
                    Message msg = new Message();
                    msg.what = 1001;
                    msg.obj = data;
                    handler.sendMessage(msg);

                }
                else {
                    showToast(result.getResultMessage());
                    if (result.getResultCode().equals("90"))
                    {
                        Intent intent = new Intent(CompInfoDetailActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }


            }
        });
    }

    /**
     * 获取企业作业范围列表信息
     */
    private void getWorkRangeList() {

        HttpMethod method = new HttpMethod();
        final HashMap<String, String> map = new HashMap<String, String>();
        map.put("keyword", getIntent().getStringExtra("ID"));
        map.put("TOKEN", myApplication.getUser().getToken());
        method.setAction(HttpMethod.SOAR_HTTP_METHOD_ACTION_EXEC);
        method.setClassPath("com.mobile.Execute");
        method.setMethodName("selectWorkRangeListInfo");
        method.setKvdata(map);
        final List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
        HttpClient.postdata(new Button(CompInfoDetailActivity.this), method, ParamCommon.ptl_server_url, true, new HttpCallBack() {
            @Override
            public void run(HttpResult result) {

                if (result.getResultCode().equals(HttpResult.SOAR_HTTP_STATUS_CODE_SUCCESS)) {
                    List<HashMap<String, String>> data1 = result.getDataSet();
                    if (data1 != null && data1.size() != 0) {
                        data.addAll(data1);
                    }
                    cargoList = data;
                    for (int i = 0;i<cargoList.size();i++)
                    {
                        HashMap<String,String> map = (HashMap<String,String>)cargoList.get(i);
                        if(cargoName.length()>0)
                        {
                            cargoName += ","+map.get("CARGO_NAME").toString();
                        }
                        else{
                            cargoName += map.get("CARGO_NAME").toString();
                        }

                    }


                }
                else {
                    showToast(result.getResultMessage());
                    if (result.getResultCode().equals("90"))
                    {
                        Intent intent = new Intent(CompInfoDetailActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
    }


    private class MyHandler extends Handler
    {
        public MyHandler(Looper looper)
        {
            super(looper);
        }

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case 999:
                    List<HashMap<String, String>> dataList = (List<HashMap<String, String>>) msg.obj;
                    if (dataList.size()>0){

                        HashMap<String,String> map = (HashMap<String,String>)dataList.get(0);
                        TextView t1 = (TextView)findViewById(R.id.t1);
                        TextView t2 = (TextView)findViewById(R.id.t2);
                        TextView t3 = (TextView)findViewById(R.id.t3);
                        TextView t4 = (TextView)findViewById(R.id.t4);
                        TextView t5 = (TextView)findViewById(R.id.t5);
                        TextView t6 = (TextView)findViewById(R.id.t6);
                        TextView t7 = (TextView)findViewById(R.id.t7);


                        t1.setText(map.get("COMPANYFULLNAME").toString());
                        t2.setText(map.get("LAWMAN").toString());
                        if (map.get("PRINCIPAL").toString().length()>0)
                        {
                            t3.setText(map.get("PRINCIPAL").toString());
                        }
                        else{
                            t3.setText("————");
                        }
                        t4.setText(map.get("FAX").toString());
                        t5.setText(map.get("EMAIL").toString());
                        if (map.get("EQUIPMENT").toString().length()>0)
                        {
                            t6.setText(map.get("EQUIPMENT").toString());
                        }
                        else{
                            t6.setText("————");
                        }
                        if (map.get("SURVEY").toString().length()>0)
                        {
                            t7.setText(map.get("SURVEY").toString());
                        }
                        else{
                            t7.setText("————");
                        }

                    }

                    break;
                case 1001:
//                    List<HashMap<String, String>> dataNewList = (List<HashMap<String, String>>) msg.obj;
//                    List<HashMap<String, String>> dataCacheList = new ArrayList<HashMap<String, String>>();
//                    if (dataNewList != null && dataNewList.size() != 0)
//                    {
//                        permitList.clear();
//                        for (int i = 0; i < dataNewList.size(); i++)
//                        {
//                            permitList.add(dataNewList.get(i));
//                        }
//                    }
//                    else
//                    {
//                        if (dataCacheList != null && dataCacheList.size() != 0)
//                        {
//                            permitList.clear();
//                            for (int i = 0; i < dataCacheList.size(); i++)
//                            {
//                                permitList.add(dataCacheList.get(i));
//                            }
//                        }
//                        else {
//                            permitList.clear();
//                        }
//                    }
//                    adapter.notifyDataSetChanged();
//                    mPullDownView.notifyDidLoad();
                   List<HashMap<String, String>> dataNewList = (List<HashMap<String, String>>) msg.obj;
                    for (int i=0;i<dataNewList.size();i++)
                    {
                        View itemll = View.inflate(CompInfoDetailActivity.this,R.layout.listitempermit,null);
                        LinearLayout item = (LinearLayout)itemll.findViewById(R.id.item_ll);
                        LinearLayout lyLL = (LinearLayout)itemll.findViewById(R.id.laycontent);
                        if (i>0)
                        {
                            LinearLayout topLL = (LinearLayout)itemll.findViewById(R.id.topLL);
                            topLL.setVisibility(View.GONE);
                        }

                        TextView t2 = (TextView)itemll.findViewById(R.id.t2);
                        TextView t3 = (TextView)itemll.findViewById(R.id.t3);
                        TextView t4 = (TextView)itemll.findViewById(R.id.t4);
                        TextView t5 = (TextView)itemll.findViewById(R.id.t5);
                        TextView t6 = (TextView)itemll.findViewById(R.id.t6);
                        TextView t7 = (TextView)itemll.findViewById(R.id.t7);
                        t2.setText(dataNewList.get(i).get("WORK_PLACE").toString());
                        t3.setText(dataNewList.get(i).get("CERT_CODE").toString());
                        t4.setText(dataNewList.get(i).get("ISSUE_DATE").toString());
                        t5.setText(dataNewList.get(i).get("END_DATE").toString());
                        t6.setText(dataNewList.get(i).get("WORK_ADDRESS").toString());
                        t7.setText(cargoName);
                        bottomell.addView(item);

                    }



                    break;
                case 1002:
                    break;
                case 1999:
                    mPullDownView.changeFoot(0, "没有更多信息了");
                    break;
                default:
                    break;
            }
        }
    }

//    class MyCustomAdapter extends BaseAdapter
//    {
//        private Context main;
//
//        private List<HashMap<String, String>> list;
//
//        public MyCustomAdapter(Context main, List<HashMap<String, String>> list)
//        {
//            this.main = main;
//            this.list = list;
//        }
//
//        public int getCount()
//        {
//            return list.size();
//        }
//
//        public Object getItem(int position)
//        {
//            return list.get(position);
//        }
//
//        public long getItemId(int position)
//        {
//            return position;
//        }
//
//        public View getView(int position, View convertView, ViewGroup parent)
//        {
//            // 如果未选择或list列表为空时直接跳出，避免NullPointException
//            if (position < 0 || list.size() <= 0)
//            {
//                return null;
//            }
////            如果为空重新导入ListView的布局文件，布局文件自定义
//            if (convertView == null)
//            {
//                convertView = LayoutInflater.from(main).inflate(R.layout.listitempermit, null);
//            }
//
//            TextView t1 = (TextView)convertView.findViewById(R.id.t1);
//            TextView t2 = (TextView)convertView.findViewById(R.id.t2);
//            TextView t3 = (TextView)convertView.findViewById(R.id.t3);
//            TextView t4 = (TextView)convertView.findViewById(R.id.t4);
//            TextView t5 = (TextView)convertView.findViewById(R.id.t5);
//            TextView t6 = (TextView)convertView.findViewById(R.id.t6);
//
//            t1.setText("作业场所："+list.get(position).get("WORK_PLACE").toString());
//            t2.setText(list.get(position).get("CERT_CODE").toString());
//            t3.setText(list.get(position).get("ISSUE_DATE").toString());
//            t4.setText(list.get(position).get("END_DATE").toString());
//            t5.setText(list.get(position).get("WORK_ADDRESS").toString());
//            t6.setText(cargoName);
//
//            return convertView;
//        }
//    }
}
